package com.menhai.personmanagement.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.menhai.personmanagement.entity.FileInfoDTO;
import com.menhai.personmanagement.plus.DateUtils;
import com.menhai.personmanagement.plus.FileUtils;
import com.menhai.personmanagement.plus.Uuid;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Controller
public class FileController {



 @Value("${profile}")//文件上传路径
 String profile;

 /**
  * @describe 上传图片 ,可自定义文件路径与文件名
  * @param file
  * @param filename
  * @Return FileInfoDTO
  * @Date 2021/12/8 11:53
  * @author shikun
  */
 @PostMapping("/upload/videoandimgurl")
 public @ResponseBody
 FileInfoDTO uploadVideoandimgurl(@RequestParam("file") MultipartFile file, String filename) {
  String Filename;
  Date date=new Date();
  String dateymd = new SimpleDateFormat("yyyy/MM/dd").format(date);
  if (StringUtils.isBlank(filename)) {
   filename = new Uuid().getUUId();
   Filename = filename + FileUtils.getFileType(file.getOriginalFilename());
  } else {
   Filename = file.getOriginalFilename();
  }
  try {
   String url="";
   String fileName = "/" + filename + ".jpg";
   String filePath = dateymd;
   File fileurl = new File(profile + filePath);
   if (!fileurl.exists()) {
    fileurl.mkdirs();
   }
   FileOutputStream out = new FileOutputStream(profile + filePath + fileName);//文件字节流
   out.write(file.getBytes());
   out.flush();
   out.close();
   url="/images/"+filePath+fileName;
   return FileInfoDTO.builder()
       .fileOrgName(file.getOriginalFilename())
       .filePath(url)
       .path(url)
       .realName(file.getOriginalFilename())
       .url(url)
       .name(Filename)
       .realPath(url)
       .build();
  } catch (Exception e) {
   log.error("上传文件出错：{}", e.getMessage());
   e.printStackTrace();
  }
  return FileInfoDTO.builder().build();
 }


 /**
  * @describe 上传PDF ,可自定义文件路径与文件名
  * @param file
  * @param filename
  * @Return com.menhai.personmanagement.entity.FileInfoDTO
  * @Date 2022/5/17 9:17
  * @author shikun
  */
 @PostMapping("/upload/loadPdfurl")
 public @ResponseBody
 FileInfoDTO loadPdfurl(@RequestParam("file") MultipartFile file, String filename) {
  String Filename;
  Date date=new Date();
  String dateymd = new SimpleDateFormat("yyyy/MM/dd").format(date);
  if (StringUtils.isBlank(filename)) {
   filename = new Uuid().getUUId();
   Filename = filename + FileUtils.getFileType(file.getOriginalFilename());
  } else {
   Filename = file.getOriginalFilename();
  }
  try {
   String url="";
   String fileName = "/" + filename + ".pdf";
   String filePath = dateymd;
   File fileurl = new File(profile + filePath);
   if (!fileurl.exists()) {
    fileurl.mkdirs();
   }
   FileOutputStream out = new FileOutputStream(profile + filePath + fileName);//文件字节流
   out.write(file.getBytes());
   out.flush();
   out.close();
   url="/images/"+filePath+fileName;
   return FileInfoDTO.builder()
       .fileOrgName(file.getOriginalFilename())
       .filePath(url)
       .path(url)
       .realName(file.getOriginalFilename())
       .url(url)
       .name(Filename)
       .realPath(url)
       .build();
  } catch (Exception e) {
   log.error("上传文件出错：{}", e.getMessage());
   e.printStackTrace();
  }
  return FileInfoDTO.builder().build();
 }


}
